package me.raih.controller.admin.user;

import me.raih.pojo.po.User;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Author Raih
 * @Time 2020/11/27  17:04
 *
 * 关于管理用户的一些操作
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminUserController {

    @Autowired
    UserService userService;

    @Autowired
    ResultInfo resultInfo;

    /**
     *  查找用户列表
     * @return 前台用户列表
     */
    @RequestMapping(value = "/user/userList")
    public String toUserMessage(){

        //List<User> userList = userService.findAllUsers();

        //session.setAttribute("USER_LIST",userList);

        return "admin/user/userList";
    }

    /**
     * 去用户编辑页面
     * @return
     */
    @RequestMapping(value = "/user/userEdit")
    public String toUserEdit(String username, Model model){

        if(username != null){
            //去除空格
            username = username.replace(" ","");
        }
        User user = userService.findUserByUsername(username);

        model.addAttribute("USER_EDIT",user);

        return "admin/user/userEdit";
    }

    /**
     * 编辑 提交编辑的内容
     * @param user
     * @return  编辑是否成功
     */
    @RequestMapping(value = "/user/editUser",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo editUser(User user){

        System.out.println(user);

        boolean flag = userService.canUpdateUserByUser(user);

        resultInfo.setFlag(flag);
        if(flag){
            resultInfo.setMessage("更新成功！");
        }else {
            resultInfo.setMessage("更新失败！");
        }

        return resultInfo;
    }

    /**
     * 前往添加页面
     * @return
     */
    @RequestMapping(value = "/user/userAdd")
    public String userAdd(){

        return "admin/user/userAdd";
    }



    @RequestMapping(value = "/user/addUser",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo addUser(User user){

        user.setRegisterTime(new Date());
        user.setPoints(0.0);
        int i = userService.canAddUser(user);
        if(i == 1){
            //注册成功
            resultInfo.setFlag(true);
            resultInfo.setMessage("添加成功！");
        }else if(i == -1){
            resultInfo.setFlag(false);
            resultInfo.setMessage("该账号已存在，请更换");
        }else {
            resultInfo.setMessage("注册失败，请联系维护人员");
            resultInfo.setFlag(false);
        }

        return resultInfo;
    }

    /**
     * 注销用户
     * @param username
     * @return 注销成功
     */
    @RequestMapping(value = "/user/deleteUser",method = RequestMethod.DELETE)
    @ResponseBody
    public ResultInfo deleteUser(@RequestParam("username")String username){
        ResultInfo resultInfo = new ResultInfo();
        boolean flag = userService.deleteUserByUsername(username);
        resultInfo.setFlag(flag);
        if (flag){
            resultInfo.setMessage("注销成功！");
        }else {
            resultInfo.setMessage("注销失败！");
        }

        return resultInfo;
    }

}
